#include "config.h"
#include <QFile>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>

Config* Config::config = nullptr;
QString Config::getApp_path()
{
    if(config == nullptr)
    {
        config = new Config();
        return config->getApp_path();
    }
    return config->App_path;
}

QString Config::getLibrary_path()
{
    if(config == nullptr)
    {
        config = new Config();
        return config->getLibrary_path();
    }
    return config->Library_path;
}

Config::Config()
{
    QFile file("./config.json");
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<("无法读取配置文件，使用默认值");
        return;
    }
    QByteArray buffer =file.readAll();
    file.close();
    
    QJsonObject obj = QJsonDocument::fromJson(buffer).object();
    QString app = obj["App_path"].toString();
    QString library = obj["Library_path"].toString();
    if(!app.isEmpty())
    {
        App_path = app;
    }
    
    if(!library.isEmpty())
    {
        Library_path = library;
    }
    
}
